package com.kou.controller;/**
 * @author SteinY
 * @create 2022-03-24 19:31
 * @description:
 */

import com.alibaba.fastjson.JSONObject;
import com.kou.pojo.User;
import com.kou.service.UserService;
import com.kou.service.UserServiceImpl;
import com.kou.utils.Base64Utils;
import com.kou.utils.Constants;
import com.kou.utils.StrUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * projectName:myshop2
 * author:SteinY
 * time:2022/3/24 19:31
 * description:
 */
@WebServlet("/user")
public class UserServlet extends BaseServlet {

    UserService userService = new UserServiceImpl();
    //验证用户唯一性
    public String check(HttpServletRequest request, HttpServletResponse response) {

        String username = request.getParameter("username");
        User user = userService.findUserByUsername(username);
        if (user==null){
            return "0";
        }else{
            return "1";
        }
    }

    public String register(HttpServletRequest request, HttpServletResponse response) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        String gender = request.getParameter("gender");

        userService.register(username,password,email,gender);

        return Constants.REDIRECT+"registerSuccess.jsp";
    }
    public String activate(HttpServletRequest request, HttpServletResponse response) {
        String email = request.getParameter("e");
        String code = request.getParameter("c");
        userService.activate(email,code);
        return Constants.REDIRECT + "index.jsp";
    }

    public String login(HttpServletRequest request, HttpServletResponse response) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String code = request.getParameter("code");
        String auto = request.getParameter("auto");
        String code0 = (String) request.getSession().getAttribute("code");
        if (code.equalsIgnoreCase(code0)){
            if(!StrUtils.empty(username)){
                request.setAttribute("msg","用户名不能为空！");
                return Constants.FORWARD + "login.jsp";
            }
            if(!StrUtils.empty(password)){
                request.setAttribute("msg","密码不能为空！");
                return Constants.FORWARD + "login.jsp";
            }
            User user = userService.login(username,password);
            if (user==null){
                request.setAttribute("msg","账号或者密码错误！");
                return Constants.FORWARD + "login.jsp";
            }else{
                if (null == auto){
                    request.getSession().setAttribute("loginUser",user);
                    return Constants.REDIRECT + "index.jsp";
                }else {
                    //session 的时间是在Tomcat启动时确定的，项目启动后无法修改
                    //故采用cookie存储,将用户名密码加密，存储在浏览器中
                    //讲对象转为JSON字符串
                    String json = JSONObject.toJSONString(user);
                    //采用密文
                    String j = Base64Utils.encode(json);
                    Cookie cookie = new Cookie(Constants.AUTO_NAME,j);
                    cookie.setPath("/");
                    cookie.setMaxAge(60*60*24*7*2);
                    //写回浏览器
                    response.addCookie(cookie);
                    request.getSession().setAttribute("loginUser",user);
                    return Constants.REDIRECT + "index.jsp";
                }

            }
        }else {
            request.setAttribute("msg","验证码错误！");
            return Constants.FORWARD + "login.jsp";
        }

    }
    public String logOut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //删除cookie和session
        request.getSession().removeAttribute("loginUser");
        Cookie cookie = new Cookie(Constants.AUTO_NAME,null);
        cookie.setPath("/");
        cookie.setMaxAge(0);
        response.addCookie(cookie);

        return Constants.REDIRECT + "index.jsp";
    }

}